package ServletMerchant;

import Dao.MctDao;
import Entity.Merchant;
import Util.Page;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;
import java.sql.SQLException;
import java.util.List;

@WebServlet(name = "Servlet.ServletMctSearch", value = "/mctsearch")
public class ServletMctSearch extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String key = request.getParameter("key");
        String username = request.getParameter("username");

//        System.out.println(key);
//        System.out.println(username);
        List<Merchant> list = null;
        try {
            list = MctDao.queryByKey(key,username);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
//        System.out.println(list);
        //获取页码
        int pagenum = 1,total = list.size();
        pagenum = Integer.parseInt(request.getParameter("pagenum"));
        Page page = new Page(pagenum,total);
        request.setAttribute("page",page);
//        System.out.println("get:"+page);

        request.setAttribute("list", list);
        request.setAttribute("username",username);
        request.getRequestDispatcher("merchant.jsp").forward(request, response);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);
    }
}
